<template>
<el-container class="home_container">
    <el-header>
        <div class="left">
            <img src="../assets/heima.png" alt="">
            <span>电商后台管理系统</span>
        </div>
        <div class="right">
            <el-button type="info" @click="logout">退出</el-button>
        </div>
    </el-header>
    <el-container>
        <el-aside :width="isCollapse?'64px':'200px'">
            <div class="toggle" @click="isCollapse=!isCollapse">|||</div>
            <!-- el-menu必须有的，只有一个 外面包裹性组件 -->
            <!-- alt + z -->
            <el-menu background-color="#333744" text-color="#fff" unique-opened :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
                <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
                    <!-- 具名插槽 -->
                    <template slot="title">
                        <!-- <i class="iconfont icon-user" v-if="item.id===125"></i>
            <i class="iconfont icon-tijikongjian" v-else-if="item.id===103"></i>
            <i class="iconfont icon-shangpin" v-else-if="item.id===101"></i>
            <i class="iconfont icon-danju" v-else-if="item.id===102"></i>
            <i class="iconfont icon-baobiao" v-else></i> -->
                        <i :class="iconsObj[item.id]"></i>&nbsp;&nbsp;
                        <span>{{item.authName}}</span>
                    </template>
                    <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="toggleActivePath('/'+subItem.path)">
                        <template slot="title">
                            <i class="el-icon-s-grid"></i>
                            <span>{{subItem.authName}}</span>
                        </template>
                    </el-menu-item>
                </el-submenu>
            </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
    </el-container>
</el-container>
</template>

<script>
// 父子路由 我们把这么多组件一样的HTML提取到父路由对应的组件上
// 钩子函数不要写太多的逻辑 封装一个函数，调用this.方法,方法可以复用
// created ajax 数据准备好了，dom没有准备好
// mounted 操作dom  数据准备好了，dom准备好
const ACTIVE_PATH = 'activePath'
export default {
  // async created () {
  //   // 请求拦截器
  //   const { data: res } = await this.$http.get('menus', { headers: { Authorization: window.sessionStorage.getItem('token') } })
  //   const { data: res } = await this.$http.get('abc', { headers: { Authorization: window.sessionStorage.getItem('token') } })
  //   console.log(res)
  // },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem(ACTIVE_PATH)
  },
  data () {
    return {
      menulist: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false, // 默认展开
      activePath: ''
    }
  },
  methods: {
    logout () {
      // 销毁token
      window.sessionStorage.clear()
      // 跳转到登录
      this.$router.push('/login')
    },
    async getMenuList () {
      const {
        data: res
      } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // console.log(res)
      this.menulist = res.data
    },
    toggleActivePath (path) {
      this.activePath = path
      window.sessionStorage.setItem(ACTIVE_PATH, path)
    }
  },
  computed: {

  }
}
</script>

<style lang="less" scoped>
.home_container {
    height: 100vh;

    .el-header {
        background-color: #373d41;
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding-left: 0;

        .left {
            img {
                vertical-align: middle;
                margin-right: 20px;
            }

            span {
                vertical-align: middle;
                color: #fff;
            }
        }
    }

    .el-aside {
        background-color: #333744;
    }

    .el-main {
        background-color: #eaedf1;
    }
}

.el-menu {
    border-right: none;
}

.toggle {
    letter-spacing: 4px;
    text-align: center;
    color: #fff;
    background-color: #4a5064;
    padding:4px 0;
    cursor: pointer;
}
</style>
